define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var AJAX = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var ECombobox = require("resource/js/component/easycombobox.js");
	ECombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"DCS_vehicleCode,DCS_constructionVehicleState,"}
	})
	var push = null;
	using(["validatebox"],function(){
		$.extend($.fn.validatebox.defaults.rules, {
			vehicleWidthResul: {
				validator: function(value){
					return /^[0-9]+(\.[0-9]{2})?$/.test(value);
				},message: "只能为正整数,两位小数"
			},
			//校验车设备Mac唯一性
			deviceVidResul:{
				validator: function(value){
                    var bol = false;
					AJAX.init().sync().success(function(code,msg,data){
						//bol = data == undefined ? true : false;
						
						if(code == 0){
							if(data < 1){
								bol = true;
							}
						} 
						
					}).error(function(code,msg,data){
					}).post(BASEPATH+'/dcs/sys/constructionVehicle/queryIsValidateMac.htm',{sectionId:$('#sectionId').val(),deviceVid:$.trim(value)});
					return bol;
				},message: "设备MAC已存在"

			},
			deviceVidUpResul:{
				validator: function(value){
					var bol = false;
					if(push.deviceVid == $.trim(value)){
						bol = true;
					}else{
						AJAX.init().sync().success(function(code,msg,data){
							if(code == 0){
								if(data < 1){
									bol = true;
								}
							}
						}).error(function(code,msg,data){
						}).post(BASEPATH+'/dcs/sys/constructionVehicle/queryIsValidateMac.htm',{sectionId:$('#sectionId').val(),deviceVid:$.trim(value)});
					}
					return bol;
				},message: "设备MAC已存在"
			}
		});
	});
	var dgutil = undefined;
	var config = {
			name:"工程车辆表",
			column:[[
                    {field:'id',title:'主键ID',width:100,align:'center',height:50,hidden:true},
                    {field:'sectionId',title:'标段ID',width:100,align:'center',height:50,hidden:true},
                    {field:'deviceVid',title:'设备MAC',width:100,align:'center',height:50,hidden:false},
                    {field:'vehicleName',title:'设备名称',width:100,align:'center',height:50,hidden:false},
				    {field:'vehicleTypeText',title:'设备型号',width:100,align:'center',height:50,hidden:false},
			    	{field:'vehicleType',title:'设备型号',width:100,align:'center',height:50,hidden:true},
			    	{field:'vehicleWidth',title:'宽度(米)',width:100,align:'center',height:50,hidden:false},
			    	{field:'frdRemark',title:'RFID设备',width:100,align:'center',height:50,hidden:false},
                    {field:'workAreaId',title:'工区',width:100,align:'center',height:50,hidden:true},
					{field:'workAreaName',title:'工区',width:100,align:'center',height:50,hidden:false},
				    {field:'updateUser',title:'最近更新人',width:100,align:'center',height:50,hidden:true},
                    {field:'stateText',title:'状态',width:100,align:'center',height:50,hidden:false},
                    {field:'state',title:'状态',width:100,align:'center',height:50,hidden:true},
                    ]]
	}
	
	var initpage = function(){
			dgutil = EUtil.ajaxDataGrid(datagridconfig);
			ECombobox.init({ ele:"#vehicleType_search", grouptype:"DCS_vehicleCode"});
       		ECombobox.init({ ele:"#state_search", grouptype:"DCS_constructionVehicleState"});
		}
	var initFiledByForm = function(rows,id){
		var validate = true;
		if(typeof rows == "boolean"){
			validate = rows;
		}
		ECombobox.init({ ele:"#vehicleType_"+id, grouptype:"DCS_vehicleCode",value:(validate?rows.vehicleType:''),
			onChange: function(validate) {
			if(validate != "" && validate == "01"){
				$("#frdRemarkId").show();
				//$("#frdRemark_"+id).textbox('textbox').validatebox('options').required = true;
			}else{
				$("#frdRemarkId").hide();
				//$("#frdRemark_"+id).textbox('textbox').validatebox('options').required = false;
			}
			}
		});
        ECombobox.init({ ele:"#state_"+id, grouptype:"DCS_constructionVehicleState",value:(validate?rows.state:'')
        });
		$("#state_"+id).combobox({
			value: "01"
		})
		var sectionId = $('#sectionId').val();//获取标段ID
		areaCombobox(sectionId,id);
	}

	function areaCombobox(sectionId,id){
		AJAX.init().sync().success(function(code,msg,data){
			//获取Data数据
			ECombobox.init({ ele:"#workAreaId_"+id,valueField:'id',remote:true, textField:'name'
			}).load(data);
			$('#workAreaId_'+id).combobox('select', data[0].id);
		}).error(function(code,msg,data){
			$.messager.alert("系统提示", msg, "error");
		}).post(BASEPATH+'/dcs/projectOverviewAction/getSectionIdAreaList.htm?sectionId='+sectionId);
	}

	var addDialogInit = function(url){
		$("#editdlg").dialog({
			title:config.name+"增加",
			href:url,
			onLoad:function(){
				initFiledByForm(false,'add');
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#editform").form("validate")){
							var sectionId = sessionStorage.getItem('sectionId');
							if(sectionId == null || sectionId == undefined){
								sectionId = '001';
							}
							var obj = $("#editform").serializeJson();
							obj.sectionId = sectionId;
							
							obj.deviceVid = $.trim(obj.deviceVid);
							
							AJAX.init().success(function(code,msg,data){
								$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
								dgutil.pager();
								$("#editdlg").dialog("close");
		    	    		}).error(function(code,msg,data){
		    	    			$.messager.alert("系统提示", msg, "error");
		    	    		}).contentType('application/json').post(BASEPATH+"/dcs/sys/constructionVehicle/saveSysConstructionVehicle.htm",JSON.stringify(obj));
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#editdlg').dialog('close');
				}
			}]
		});
		$("#editdlg").dialog("open");
	}
	var updDialogInit = function(url,row){
		$("#editdlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'修改',
			href:url,
			onLoad:function(){
				push = row;
				initFiledByForm(row,'upd');
				if(row.vehicleType.toString() == "01"){
					$("#frdRemarkId").show();
				}else{
					$("#frdRemarkId").hide();
				}
				$("#editform").form("load",row);
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#editform").form("validate")){
							var sectionId = sessionStorage.getItem('sectionId');
							if(sectionId == null || sectionId == undefined){
								sectionId = '001';
							}
							var obj = $("#editform").serializeJson();
							obj.sectionId = sectionId;
							
							obj.deviceVid = $.trim(obj.deviceVid);
							
							AJAX.init().success(function(code,msg,data){
								$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
								dgutil.pager();
								$("#editdlg").dialog("close");
		    	    		}).error(function(code,msg,data){
		    	    			$.messager.alert("系统提示", msg, "error");
		    	    		}).contentType('application/json').post(BASEPATH+'/dcs/sys/constructionVehicle/updateSysConstructionVehicle.htm',JSON.stringify(obj));
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#editdlg').dialog('close');
				}
			}]
		});
		$("#editdlg").dialog("open");
	}
	var delInit = function(url,rows){
		var checks = [];
		for(var key in rows){
			var id = '' +rows[key].id;
			checks.push(id);
		}
		EUtil.ajaxHandler(url,{checkeds:checks.join(",")},function(){
			dgutil.delRow(rows);
		});
	}
	module.exports = {
			trans:function(){
				var buttons ={ 
						title:{
							_class:"",
							text:"工程车辆表列表"
						},
						btn:[
						     {
						    	 iconCls: 'icon-search',
						    	 url:'/dcs/sys/constructionVehicle/querySysConstructionVehicleByPage.htm',
						    	 text:"查询",
						    	 addClass:'l-btn-display',
						    	 handler: function(){dgutil.pager(1);}
						     },
						     {
						    	 iconCls: 'icon-add',
						    	 addClass:'l-btn-display',
						    	 text:"添加",
						    	 url:'/view/dcs/sys/constructionVehicle/sysconstructionvehicleadd',
						    	 handler: addDialogInit
						     },
						     {
						    	 iconCls: 'icon-edit',
						    	 url:'/view/dcs/sys/constructionVehicle/sysconstructionvehicleupd',
						    	 text:"修改",
						    	 addClass:'l-btn-display',
						    	 needRow:true,
						    	 handler: updDialogInit
						     },
						     {
						    	 iconCls: 'icon-remove',
						    	 url:'/dcs/sys/constructionVehicle/deleteSysConstructionVehicle.htm',
						    	 text:"删除",
						    	 addClass:'l-btn-display',
						    	 needRows:true,
						    	 isCheck:true,
						    	 handler: delInit
						     }
						     ]};
				datagridconfig = {
						fitColumns:true,
						singleSelect:false,
						striped:true,
						autoRowHeight:false,
						fit: true,
						toolbar:buttons,
						columns:config.column,
						eid:"dg",
						url:BASEPATH+'/dcs/sys/constructionVehicle/querySysConstructionVehicleByPage.htm',
						args:"searchform"
				};
				
				using(["parser","dialog","datagrid" ,"combo","combobox"],function(){
					initpage();
				});
			}
	}
});
